package com.pfs.internal.timestamp.services;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.springframework.beans.factory.annotation.Autowired;

import com.pfs.internal.timestamp.actions.TimeStampAction;
import com.pfs.internal.timestamp.entities.CardHolder;
import com.pfs.internal.timestamp.entities.History;
import com.pfs.internal.timestamp.entities.Leave;
import com.pfs.internal.timestamp.entities.TimeStamp;
import com.pfs.internal.timestamp.entities.TrackDay;
import com.pfs.internal.timestamp.repositories.CardHolderRepository;
import com.pfs.internal.timestamp.repositories.HistoryRepositoryImpl;
import com.pfs.internal.timestamp.utils.TimeStampUtil;

public class TrackServiceImpl implements TrackService {
	@Autowired
	private HistoryRepositoryImpl historyRepository;
	@Autowired
	private CardHolderRepository cardHolderRepository;

	public HistoryRepositoryImpl getHistoryRepository() {
		return historyRepository;
	}

	public void setHistoryRepository(HistoryRepositoryImpl historyRepository) {
		this.historyRepository = historyRepository;
	}

	public List<History> searchTracksByHistory(Map<String, Object> mapParams) {
		return historyRepository.searchTracks(mapParams);
	}

	public CardHolderRepository getCardHolderRepository() {
		return cardHolderRepository;
	}

	public void setCardHolderRepository(CardHolderRepository cardHolderRepository) {
		this.cardHolderRepository = cardHolderRepository;
	}

	public List<CardHolder> searchTracksByCardHolder(Map<String, Object> mapParams) {
		return cardHolderRepository.searchTracks(mapParams);
	}

	public List<TrackDay> calculateTrackDays(Map<String, Object> mapParams, List<Leave> leaves, List<Date> outDates) {
		Calendar c = Calendar.getInstance();
		Date date = (Date) mapParams.get(TimeStampAction.FROM_DATE);
		c.setTime(date);
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH);
		List<History> histories = historyRepository.searchTracks(mapParams);
		Map<String, List<TrackDay>> mapTrackDays = TimeStampUtil.calculate(histories, year, month, leaves, outDates);
		List<TrackDay> trackDays = new ArrayList<TrackDay>();
		for (Map.Entry<String, List<TrackDay>> entry : mapTrackDays.entrySet()) {
			// Integer key = entry.getKey();
			List<TrackDay> trackDays2 = entry.getValue();
			trackDays.addAll(trackDays2);
		}
		System.out.println("trackdays-size=" + trackDays.size());
		return trackDays;
	}
	
	public List<TimeStamp> calculateTimeStamps(List<TrackDay> listDtl) {
		Map<String, List<TrackDay>> mapDtl = new TreeMap<String, List<TrackDay>>();
		for(TrackDay trackDay : listDtl){
			String username = trackDay.getUsername();
			if (mapDtl.containsKey(username)) {
				List<TrackDay> list = mapDtl.get(username);
				list.add(trackDay);
			} else {
				List<TrackDay> list = new ArrayList<TrackDay>();
				list.add(trackDay);
				mapDtl.put(username, list);
			}
		}
		List<TimeStamp> lstHdr= TimeStampUtil.calculateHdr(mapDtl);
		return lstHdr;
	}

}
